package com.neborosoft.lib.collections.list;

import java.util.Collection;
import java.util.List;

/**
 * Project: FlyingDog
 * User: stikhonenko
 * Date: 12/10/12
 * Time: 3:10 PM
 */
public abstract class PrioritySortableList<T> extends BasicListConcatenation<T> {
    private int priorityCount;
    private Object[] lists;

    @Override
    protected List<T> getList(int index) {
        return (List<T>)lists[index];
    }

    @Override
    protected int getListsCount() {
        return getPriorityCount();
    }

    public PrioritySortableList(int priorityCount) {
        this.priorityCount = priorityCount;
        lists = new Object[priorityCount];
    }

    public int getPriorityCount() {
        return priorityCount;
    }

    @Override
    public T set(int i, T value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean addAll(Collection<? extends T> collection) {
        boolean listModified = false;

        for(T object : collection){
            if(add(object)){
                listModified = true;
            }
        }

        return listModified;
    }

    @Override
    public boolean addAll(int i, Collection<? extends T> collection) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean add(T object) {
        int priority = getPriority(object);
        List<T> list = getList(priority);
        return list.add(object);
    }

    @Override
    public void add(int i, T object) {
        throw new UnsupportedOperationException();
    }

    protected abstract int getPriority(T object);
}
